package com.qqt.csr.web.vo.resp;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.qqt.csr.common.acl.aics.resp.ChannelInfoDTO;
import com.qqt.csr.common.acl.aics.resp.CsMemberInfoDTO;
import com.qqt.csr.common.enums.YesOrNotEnum;
import com.qqt.csr.common.utils.JsonUtil;
import com.qqt.csr.im.dto.ConversationDTO;
import com.qqt.csr.im.entity.Account;
import com.qqt.csr.workbench.entity.SkillSetConfig;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serial;
import java.io.Serializable;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@Schema(name = "VisitorConversationInfoRespVo", description = "访客转人工的会话信息")
public class VisitorConversationInfoRespVO implements Serializable {
    @Serial
    private static final long serialVersionUID = -7182566496999661831L;

    @Schema(description = "客服id；onlineCs=1时，有值")
    private Long csId;

    @Schema(description = "客服名称")
    private String csName;

    @Schema(description = "客服头像")
    private String csHeadImg;

    @Schema(description = "会话id")
    private String conversationId;

    @Schema(description = "用户IM账号")
    private Long userImAccountId;

    @Schema(description = "客服IM账号")
    private Long csImAccountId;

    // @Schema(description = "渠道id")
    // private String channelId;
    //
    // @Schema(description = "渠道名称")
    // private String channelName;
    //
    @Schema(description = "是否有在线客服：0 否；1 是；2 排队等待分配")
    private Integer onlineCs;
    //
    // @Schema(description = "问候语")
    // private String greetingsTip;
    //
    // @Schema(description = "无坐席在线策略：0 仅退出")
    // private Integer noSeatPolicy;
    //
    // @Schema(description = "无坐席提示")
    // private String noSeatTip;
    //
    @Schema(description = "会话结束提示语")
    private String endSessionTip;

    /**
     * 无坐席时，构建的信息
     *
     * @param skillSetConfig
     * @return
     */
    public static VisitorConversationInfoRespVO buildNotSeatCs(SkillSetConfig skillSetConfig) {
        return VisitorConversationInfoRespVO.builder()
                .onlineCs(YesOrNotEnum.NOT.getValue())
                .build();
    }

    /**
     * 构建失败提示
     *
     * @param tip
     * @return
     */
    public static VisitorConversationInfoRespVO buildFail(String tip) {
        return VisitorConversationInfoRespVO.builder()
                .onlineCs(YesOrNotEnum.NOT.getValue())
                .endSessionTip(tip)
                .build();
    }

    public static VisitorConversationInfoRespVO buildBoundConversationSuccess(ConversationDTO existConversation, Account csAccount, CsMemberInfoDTO csMemberInfoDTO,
                                                                              ChannelInfoDTO channelInfo, SkillSetConfig skillSetConfig) {
        String csNickName = StringUtils.EMPTY;
        String csHeadImg = StringUtils.EMPTY;
        Integer appearanceType = null;
        JSONObject conversationExt = JsonUtil.toObject(existConversation.getExt(), JSONObject.class);
        if (conversationExt != null) {
            appearanceType = conversationExt.getInteger("appearanceType");
        }
        if (appearanceType != null) {
            if (appearanceType == 1) {
                csNickName = skillSetConfig.getCsNickname();
                csHeadImg = skillSetConfig.getCsHeadImg();
            } else {
                csNickName = csMemberInfoDTO.getNickName();
                csHeadImg = csMemberInfoDTO.getHeadImg();
            }
        } else if (channelInfo.isSkillSetAppearanceType()) {
            csNickName = skillSetConfig.getCsNickname();
            csHeadImg = skillSetConfig.getCsHeadImg();
        } else if (channelInfo.isCsAppearanceType()) {
            csNickName = csMemberInfoDTO.getNickName();
            csHeadImg = csMemberInfoDTO.getHeadImg();
        }
        return VisitorConversationInfoRespVO.builder()
                .userImAccountId(existConversation.getInitiator().getId())
                .conversationId(existConversation.getId())
                .csName(csNickName)
                .csHeadImg(csHeadImg)
                .csImAccountId(csAccount.getId())
                .csId(csMemberInfoDTO.getUserId())
                .onlineCs(YesOrNotEnum.YES.getValue())
                .build();
    }
}
